//
//  XLGSendQueueDBOperation.m
//  etionCBLogic
//
//  Created by wjxfhxy on 15-2-11.
//  Copyright 2015 GuangZhouXuanWu. All rights reserved.
//

#import "XLGSendQueueDBOperation.h"

NSString *const SendQueueTable = @"sendqueue";

@implementation XLGSendQueueDBOperation
{
	XMSSQLiteHelper* _sqlHelper;
}

- (id)initWithTablePath:(NSString*)tablePath
{
	self = [super init];
    
	_sqlHelper = [XMSSQLiteHelper new];
    
    [_sqlHelper openTable:tablePath flags:SQLITE_OPEN_READWRITE| SQLITE_OPEN_FULLMUTEX];
    
	return self;
}

-(void)dealloc
{
	if(_sqlHelper != nil)
		[_sqlHelper closeTable];
	[_sqlHelper release_mm];
	superdealloc_mm;
}

-(NSInteger)addSendQueueIndex:(XLGSendQueueIndex *)mi
{    
    XMSSQLiteHelper *sqlHelper = [[[XMSSQLiteHelper alloc]initWithSQLiteHelper:_sqlHelper] autorelease_mm];
    NSArray *array = @[[CSQLKv SQLkvWithKey:@"extendid" value:mi.extendID type:ESQLKvVarTypeString],
                       [CSQLKv SQLkvWithKey:@"sendtype" value:@(mi.sendType) type:ESQLKvVarTypeInt],
                       [CSQLKv SQLkvWithKey:@"totalbyte" value:@(mi.totalByte) type:ESQLKvVarTypeInt],
                       [CSQLKv SQLkvWithKey:@"sendbyte" value:@(mi.sendByte) type:ESQLKvVarTypeInt],
                       [CSQLKv SQLkvWithKey:@"submittime" value:mi.submitTime type:ESQLKvVarTypeDateTime],
                       [CSQLKv SQLkvWithKey:@"sendtime" value:mi.sendTime type:ESQLKvVarTypeDateTime],
                       [CSQLKv SQLkvWithKey:@"sendstatus" value:@(mi.sendStatus) type:ESQLKvVarTypeInt],
                       [CSQLKv SQLkvWithKey:@"serialno" value:@(mi.serialNo) type:ESQLKvVarTypeInt],
                       [CSQLKv SQLkvWithKey:@"title" value:mi.title type:ESQLKvVarTypeString],
                       [CSQLKv SQLkvWithKey:@"content" value:mi.content type:ESQLKvVarTypeString],
                       [CSQLKv SQLkvWithKey:@"targettype" value:@(mi.targetType) type:ESQLKvVarTypeInt]];
    NSInteger nResult = [sqlHelper runInsert:SendQueueTable kv:array];
    
    return nResult;
}

- (void)getSendQueueIndexByCondition:(NSString*)condition completion:(void(^)(NSInteger nResult, NSArray *items))completion
{
    XMSSQLiteHelper *sqlHelper = [[[XMSSQLiteHelper alloc]initWithSQLiteHelper:_sqlHelper] autorelease_mm];
     
    NSString *sql = [NSString stringWithFormat:@"select extendid,sendtype,totalbyte,sendbyte,submittime,sendtime,sendstatus,serialno,title,content,targettype from %@",SendQueueTable];
    
    if(condition.length > 0)
        sql = [NSString stringWithFormat:@"%@ %@",sql,condition];

    NSMutableArray *array = [[NSMutableArray new] autorelease_mm];
    
    NSInteger nResult = [sqlHelper selectTable:[sql UTF8String] gettingTable:^(BOOL *bStop)
    {
        XLGSendQueueIndex *index = [[XLGSendQueueIndex new]autorelease_mm];
        [array addObject:index];
        
        index.extendID = [sqlHelper getFieldNSString:0];
        index.sendType = [sqlHelper getFieldInt:1];
        index.totalByte = [sqlHelper getFieldInt:2];
        index.sendByte = [sqlHelper getFieldInt:3];
        index.submitTime = [sqlHelper getFieldNSDate:4];
        index.sendTime = [sqlHelper getFieldNSDate:5];
        index.sendStatus = [sqlHelper getFieldInt:6];
        index.serialNo = [sqlHelper getFieldInt:7];
        index.title = [sqlHelper getFieldNSString:8];
        index.content = [sqlHelper getFieldNSString:9];
        index.targetType = [sqlHelper getFieldInt:10];
    }];
    
    completion(nResult,array);
}

- (void)getSendQueueIndexCompletion:(void (^)(NSInteger nResult, NSArray *index))completion
{
    [self getSendQueueIndexByCondition:@"where targettype = 1 order by serialno asc" completion:completion];
}

- (void)getFinishSendIndexCompletion:(void(^)(NSInteger nResult, NSArray *index))completion
{
    [self getFinishSendIndex:^NSString *(NSString *condition)
    {
        if([condition isEqualToString:@"limit"])
            return @"limit 100";
        return nil;
    } completion:completion];
}

- (void)getFinishSendIndex:(NSString*(^)(NSString *condition))condition completion:(void(^)(NSInteger nResult, NSArray *index))completion
{
    NSMutableString *cStr = [[NSMutableString new] autorelease_mm];
    [cStr appendString:@"where targettype = 2"];
    
    NSMutableArray *c = [NSMutableArray new];
    
    NSString *value = condition(@"submittime");
    if(value != nil)
        [c addObject:value];
    
    if(c.count > 0)
    {
        [cStr appendString:@" and "];
        [cStr appendString:[NSString megerStringWithTag:c tag:@" and "]];
    }
    
    [cStr appendString:@" order by submittime desc "];
    
    value = condition(@"limit");
    if(value != nil)
    {
        [cStr appendString:value];
    }
    
    [self getSendQueueIndexByCondition:cStr completion:completion];
}

- (NSInteger)modifySendedSendIndex:(NSString*)extendID
{
    XMSSQLiteHelper *sqlHelper = [[[XMSSQLiteHelper alloc]initWithSQLiteHelper:_sqlHelper] autorelease_mm];
    
    CSQLKv *condition = [CSQLKv SQLkvWithKey:@"extendid" value:extendID type:ESQLKvVarTypeString];
    CSQLKv *value1 = [CSQLKv SQLkvWithKey:@"sendtime" value:[NSDate date] type:ESQLKvVarTypeDateTime];
    CSQLKv *value2 = [CSQLKv SQLkvWithKey:@"serialno" value:@(9999) type:ESQLKvVarTypeInt];
    CSQLKv *value3 = [CSQLKv SQLkvWithKey:@"targettype" value:@(XLGSendQueueTargetTypeFinish) type:ESQLKvVarTypeInt];
    CSQLKv *value4 = [CSQLKv SQLkvWithKey:@"sendstatus" value:@(XLGSendItemStatusTypeSended) type:ESQLKvVarTypeInt];
    
    return [sqlHelper runUpdate:SendQueueTable valueKv:@[value1,value2,value3,value4] conditionKv:@[condition]];
}

- (NSInteger)modifySendIndexSerialNo:(NSInteger(^)(NSInteger(^modify)(NSUInteger serialNo,NSString *extendID)))block
{
    XMSSQLiteHelper *sqlHelper = [[[XMSSQLiteHelper alloc]initWithSQLiteHelper:_sqlHelper] autorelease_mm];
    NSInteger nResult = [sqlHelper transExce:^NSInteger
    {
        NSInteger(^modify)(NSUInteger index,NSString *extendID) = ^NSInteger(NSUInteger serialNo,NSString *extendID)
        {
            CSQLKv *value = [CSQLKv SQLkvWithKey:@"serialno" value:@(serialNo) type:ESQLKvVarTypeInt];
            CSQLKv *condition = [CSQLKv SQLkvWithKey:@"extendid" value:extendID type:ESQLKvVarTypeString];
            return [sqlHelper runUpdate:SendQueueTable valueKv:@[value] conditionKv:@[condition]];
        };
        return block(modify);
    }];
    return nResult;
}

- (NSInteger)modifySendIndexStatus:(NSInteger(^)(NSInteger(^modify)(NSUInteger serialNo,NSString *extendID,NSUInteger sendStatus)))block
{
    XMSSQLiteHelper *sqlHelper = [[[XMSSQLiteHelper alloc]initWithSQLiteHelper:_sqlHelper] autorelease_mm];
    
    NSInteger nResult = [sqlHelper transExce:^NSInteger
    {
        NSInteger(^modify)(NSUInteger serialNo,NSString *extendID,NSUInteger sendStatus) = ^NSInteger(NSUInteger serialNo,NSString *extendID,NSUInteger sendStatus)
        {
            CSQLKv *condition = [CSQLKv SQLkvWithKey:@"extendid" value:extendID type:ESQLKvVarTypeString];
            CSQLKv *value1 = [CSQLKv SQLkvWithKey:@"serialno" value:@(serialNo) type:ESQLKvVarTypeInt];
            CSQLKv *value2 = [CSQLKv SQLkvWithKey:@"sendstatus" value:@(sendStatus) type:ESQLKvVarTypeInt];
            return [sqlHelper runUpdate:SendQueueTable valueKv:@[value1,value2] conditionKv:@[condition]];
        };
        return block(modify);
    }];
    return nResult;
}

- (NSInteger)deleteSendIndexWithExtendID:(NSArray*)extendIDs
{
    XMSSQLiteHelper* sqlHelper=[[[XMSSQLiteHelper alloc]initWithSQLHandler:_sqlHelper.m_sqlhandle] autorelease_mm];
    
    NSString *condition = [NSString megerStringWithTag:extendIDs value:^NSString *(id obj)
                           {
                               return  [NSString stringWithFormat:@"extendid = '%@'", obj];
                           } tag:@" or "];

	return [sqlHelper runDelete:SendQueueTable condition:condition];
}

- (NSInteger)deleteFinishSendIndex
{
    return [self deleteFinishSendIndex:^NSString *(NSString *condition)
    {
        return nil;
    }];
}

- (NSInteger)deleteFinishSendIndex:(NSString *(^)(NSString *))condition
{
    XMSSQLiteHelper* sqlHelper=[[[XMSSQLiteHelper alloc]initWithSQLHandler:_sqlHelper.m_sqlhandle] autorelease_mm];
    
    NSMutableString *cStr = [[NSMutableString new] autorelease_mm];
    [cStr appendString:@"targettype = 2"];
    
    NSMutableArray *c = [[NSMutableArray new] autorelease_mm];
    NSString *value = condition(@"submittime");
    if(value != nil)
        [c addObject:value];
    
    if(c.count > 0)
    {
        [cStr appendString:@" and "];
        [cStr appendString:[NSString megerStringWithTag:c tag:@" and "]];
    }
    
    return [sqlHelper runDelete:SendQueueTable condition:cStr];
}

@end
